import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from "vuex-persistedstate"

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    item:{},//详情页的数据
    list:[],//购物车的数据
    price:0,
    checked:false,
    login:{},
  },
  getters: {
  },
  mutations: {
    onItem(state,val){
      // console.log(state);
      state.item=val;
      // console.log(state.item);
    },
    onList(state,val){
      // state.list.push(val);
      let a=state.list.findIndex(item=> item.id==val.id)
      if(a == -1){
        Vue.set(val,"num",1);
        state.list.push(val);
        // console.log(a);
        // console.log(state.list);
      }else{
        state.list[a].num++;
        // console.log(a);
      }
    },
    onJia(state,val){
      state.list.find(item => {
        if(item.id==val.id){
          item.num+=1;
        }
      });

      // onMonye();
    },
    onClick(state,val){
      let arr=state.list.findIndex(item=>{
        return item.id==val.id;
      })  
      if(state.list[arr].stockout_reserve){
        state.list[arr].stockout_reserved=!state.list[arr].stockout_reserved;
      }
      let bool=state.list.filter(item=>{
        return item.stockout_reserved==true;
      })
      if(state.list.length==bool.length){
          state.list.forEach(item =>{
              state.checked=true;
              return item.stockout_reserved=true;
          });
          
      }else{
          state.checked=false;
      }
      // onMonye();
    },
    checkAll(state){
      state.checked=!state.checked;
      state.list.forEach(item=>{
        return item.stockout_reserved = state.checked;
      })
      // onMonye();
    },
    onMonye(state,val){
      console.log(val);
      state.price=val;
    },
    onLogin(state,val){
      state.login=val;
    }
  },
  actions: {
  },
  modules: {

  },
  plugins:[createPersistedState({
    key:"list",
  })]
})
